package cn.fresh.xuanyuan.module.infra.service.column;

import cn.fresh.xuanyuan.framework.common.exception.ErrorCode;
import cn.fresh.xuanyuan.framework.common.exception.ServiceException;
import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.module.infra.dal.dataobject.column.MallColumnDO;
import cn.fresh.xuanyuan.module.infra.dal.dataobject.column.MallColumnProductDO;
import cn.fresh.xuanyuan.module.infra.dal.mysql.column.MallColumnMapper;
import cn.fresh.xuanyuan.module.infra.dal.mysql.column.MallColumnProductMapper;
import cn.fresh.xuanyuan.module.infra.dto.MallColumnDetailDto;
import cn.fresh.xuanyuan.module.infra.dto.MallColumnDto;
import cn.fresh.xuanyuan.module.infra.dto.MallColumnFormDto;
import cn.fresh.xuanyuan.module.infra.vo.MallColumnVo;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Validated
public class MallColumnServiceImpl implements MallColumnService {

    @Autowired
    private MallColumnMapper mallColumnMapper;

    @Autowired
    private MallColumnProductMapper mallColumnProductMapper;

    @Override
    public PageResult<MallColumnDO> getPageList(String name, String type, PageParam pageParam) {
        return mallColumnMapper.selectPage(name, type, pageParam);
    }

    @Override
    public void addColumn(MallColumnFormDto mallColumn) {
        LambdaQueryWrapper<MallColumnDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallColumnDO::getTitle, mallColumn.getTitle()).eq(MallColumnDO::getType, mallColumn.getType());
        if (mallColumnMapper.selectCount(queryWrapper) > 0) {
            throw new ServiceException(new ErrorCode(400, "栏目已存在"));
        }
        MallColumnDO mallColumnDO = new MallColumnDO();
        BeanUtils.copyProperties(mallColumn, mallColumnDO);
        mallColumnDO.setCreateTime(LocalDateTime.now());
        mallColumnMapper.insert(mallColumnDO);
    }

    @Override
    public void updateColumn(MallColumnFormDto mallColumn) {
        LambdaQueryWrapper<MallColumnDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallColumnDO::getTitle, mallColumn.getTitle()).eq(MallColumnDO::getType, mallColumn.getType());
        MallColumnDO exist = mallColumnMapper.selectOne(queryWrapper);
        if (exist != null && !exist.getId().equals(mallColumn.getId())) {
            throw new ServiceException(new ErrorCode(400, "栏目已存在"));
        }
        MallColumnDO mallColumnDO = new MallColumnDO();
        BeanUtils.copyProperties(mallColumn, mallColumnDO);
        mallColumnDO.setUpdateTime(LocalDateTime.now());
        mallColumnMapper.updateById(mallColumnDO);
    }

    @Override
    public List<MallColumnDto> getWxColumnList() {
        LambdaQueryWrapper<MallColumnDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallColumnDO::getType, "wx");
        List<MallColumnDO> list = mallColumnMapper.selectList(queryWrapper);
        List<MallColumnDto> data = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)) {
            for (MallColumnDO mallColumnDO : list) {
                MallColumnDto mallColumnDto = new MallColumnDto();
                BeanUtils.copyProperties(mallColumnDO, mallColumnDto);
                data.add(mallColumnDto);
            }
        }
        return data;
    }

    @Override
    public List<MallColumnDto> getH5ColumnList() {
        LambdaQueryWrapper<MallColumnDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallColumnDO::getType, "h5");
        List<MallColumnDO> list = mallColumnMapper.selectList(queryWrapper);
        List<MallColumnDto> data = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)) {
            for (MallColumnDO mallColumnDO : list) {
                MallColumnDto mallColumnDto = new MallColumnDto();
                BeanUtils.copyProperties(mallColumnDO, mallColumnDto);
                data.add(mallColumnDto);
            }
        }
        return data;
    }

    @Override
    public MallColumnDetailDto getColumnDetail(Integer id) {
        MallColumnDetailDto mallColumnDetailDto = new MallColumnDetailDto();
        MallColumnDO mallColumnDO = mallColumnMapper.selectById(id);
        if(mallColumnDO != null) {
            LambdaQueryWrapper<MallColumnProductDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MallColumnProductDO::getColumnId, mallColumnDO.getId()).eq(MallColumnProductDO::getDeleted, false);
            Long total = mallColumnProductMapper.selectCount(queryWrapper);
            BeanUtils.copyProperties(mallColumnDO, mallColumnDetailDto);
            mallColumnDetailDto.setProductNum(total);
        }
        return mallColumnDetailDto;
    }

    @Override
    public List<MallColumnVo> wxIndex() {
        LambdaQueryWrapper<MallColumnDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallColumnDO::getType,"wx").eq(MallColumnDO::getStatus, 0).eq(MallColumnDO::getDeleted,false).orderByAsc(MallColumnDO::getSort);
        List<MallColumnDO> list = mallColumnMapper.selectList(queryWrapper);
        List<MallColumnVo> result = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)) {
            list.forEach(o -> {
                MallColumnVo v = new MallColumnVo();
                BeanUtils.copyProperties(o,v);
                result.add(v);
            });
        }
        return result;
    }
}
